package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.HorizontalScrollView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;
import com.sgb.link.utils.SizeUtils;
import org.jetbrains.annotations.NotNull;

public class TreeIndicatorView extends View {

    private Paint mBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private RectF mBgRect = new RectF();
    private float mRadius = 0f;
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private RectF mRect = new RectF();
    private int viewWidth = 0;
    private int mBgColor = Color.parseColor("#e5e5e5");
    private int mIndicatorColor = Color.parseColor("#EF4034");
    private float ratio = 0.5f;   //长度比例
    private float progress = 0f; //滑动进度比例

    public TreeIndicatorView(Context context) {
        this(context, null);
    }

    public TreeIndicatorView(Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TreeIndicatorView(Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mBgPaint.setColor(mBgColor);
        mBgPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(mIndicatorColor);
        mPaint.setStyle(Paint.Style.FILL);
    }

    /*
     * 设置进度
     * */
    public void setProgress(float progress) {
        this.progress = progress;
        invalidate();
    }

    /*
     * 设置指示器所占的长度比例
     * */
    private void setRatio(float mRatio) {
        this.ratio = mRatio;
        invalidate();
    }

    /**
     * 设置指示器背景进度条的颜色
     *
     * @param color 背景色
     */
    public void setBgColor(int color) {
        mBgPaint.setColor(color);
        invalidate();
    }

    /**
     * 设置指示器的颜色
     *
     * @param color 指示器颜色
     */
    public void setIndicatorColor(int color) {
        mPaint.setColor(color);
        invalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        viewWidth = w;
        mBgRect.set(0f, 0f, w * 1f, h * 1f);
        mRadius = h / 2f;
    }

    /**
     * 绑定recyclerView
     */
    public void bindRecyclerView(RecyclerView recyclerView) {
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull @NotNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(@NonNull @NotNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int offsetX = recyclerView.computeHorizontalScrollOffset(); //已经向下滚动的距离，为0时表示已处于顶部。
                int range = recyclerView.computeHorizontalScrollRange();  //整体的高度，注意是整体，包括在显示区域之外的
                int extend = recyclerView.computeHorizontalScrollExtent(); //当前RcyclerView显示区域的高度。水平列表屏幕从左侧到右侧显示范围
                float mProgress = offsetX * 1.0f / (range - extend);
                setProgress(mProgress); //设置滚动距离所占比例
            }
        });

        recyclerView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                int range = recyclerView.computeHorizontalScrollRange();
                int extend = recyclerView.computeHorizontalScrollExtent();
                float mRatio = extend * 1f / range;
                setRatio(mRatio);    //设置指示器所占的长度比例
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    /**
     * totalWidth 整体的高度，注意是整体，包括在显示区域之外的
     */
    public void bindHorizontalScrollView(HorizontalScrollView scrollView, Context context, int totalWidth) {
        scrollView.setOnScrollChangeListener(new OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                //int width = scrollView.getMeasuredWidth(); //整体的高度，注意是整体，包括在显示区域之外的
                int extend = SizeUtils.getScreenWidth(context); //当前显示区域的宽度。水平列表屏幕从左侧到右侧显示范围
                float mProgress = scrollX * 1.0f / (totalWidth - extend);
                setProgress(mProgress); //设置滚动距离所占比例
            }
        });
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制背景
        canvas.drawRoundRect(mBgRect, mRadius, mRadius, mBgPaint);
        //计算指示器的长度和位置
        float leftOffset = viewWidth * (1f - ratio) * progress;
        float left = mBgRect.left + leftOffset;
        float right = left + viewWidth * ratio;
        mRect.set(left, mBgRect.top, right, mBgRect.bottom);
        //绘制指示器
        canvas.drawRoundRect(mRect, mRadius, mRadius, mPaint);
    }
}
